package cn.hackcloud.mutithread.mutilthread.lock5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyService {
	private Lock lock;

	public MyService(boolean isFair) {
		lock = new ReentrantLock(isFair);
	}

	public void serviceMehtod() {
		lock.lock();
		try {
			System.out.println(Thread.currentThread().getName() + "获得锁定！");
		} finally {
			lock.unlock();
		}
	}

	public static void main(String[] args) {
		final MyService myService = new MyService(false);
		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				myService.serviceMehtod();
				System.out.println(Thread.currentThread().getName() + "运行了！");
			}
		};
		Thread[] threads = new Thread[10];
		for (int i = 0; i < threads.length; i++) {
			threads[i] = new Thread(runnable);
		}
		for (int i = 0; i < threads.length; i++) {
			threads[i].start();
		}
	}
}
